require 'erb'

class BGPBase < Base

  attr_accessor :step, :transport, :base_options, :base_transports, :base_variables, :message

  def initialize(step, filename, params = {}, transport='BGP')
   super()
    @transport = transport
    @step = step
    
   @origin_flags = params['origin_flags'] or '0h40'
   @origin_length = params['origin_length'] or '1'
   @origin_value = params['origin_value'] or '0'
   @as_path_flags = params['as_path_flags'] or '0h40'
   @as_path_length = params['as_path_length'] or '4'
   @path_segment_type = params['path_segment_type'] or '02'
   @path_segment_length = params['path_segment_length'] or '01'
   @path_segment_value = params['path_segment_value'] or '65111'
   @next_hop_flags = params['next_hop_flags'] or '0h40'
   @next_hop_length = params['next_hop_length'] or '04'
   @next_hop_value = params['next_hop_value'] or '192.168.120.210'
   @local_pref_flags = params['local_pref_flags'] or '0h40'
   @local_pref_length = params['local_pref_length'] or '0h04'
   @local_pref_value = params['local_pref_value'] or '100'
   @communities_flags = params['communities_flags'] or '0hc0'
   @communities_length = params['communities_length'] or '0h12'
   @communities_value_1 = params['communities_value_1'] or '0xffffff01'
   @communities_value_2 = params['communities_value_2'] or '0xffffff02'
   @communities_value_3 = params['communities_value_3'] or '0xffffff03'
   @originator_flags = params['originator_flags'] or '0h80'
   @originator_length = params['originator_length'] or '4'
   @originator_id = params['originator_id'] or '01020304'
   @cluster_list_flags = params['cluster_list_flags'] or '0h80'
   @cluster_list_length = params['cluster_list_length'] or '4'
   @cluster_list = params['cluster_list'] or '04030201'
   @ext_communities_flags = params['ext_communities_flags'] or '0hc0'
   @ext_communities_length = params['ext_communities_length'] or '16'
   @ext_communities_route_target = params['ext_communities_route_target'] or '0002fde800000001'
   @ext_communities_route_origin = params['ext_communities_route_origin'] or '000301d000000003'

   @base_options = "
     $origin_flags = '<%= @origin_flags %>'
     $origin_length = '<%= @origin_length %>'
     $origin_value = '<%= @origin_value %>'
     $as_path_flags = '<%= @as_path_flags %>'
     $as_path_length = '<%= @as_path_length %>'
     $path_segment_type = '<%= @path_segment_type %>'
     $path_segment_length = '<%= @path_segment_length %>'
     $path_segment_value = '<%= @path_segment_value %>'
     $next_hop_flags = '<%= @next_hop_flags %>'
     $next_hop_length = '<%= @next_hop_length %>'
     $next_hop_value = '<%= @next_hop_value %>'
     $local_pref_flags = '<%= @local_pref_flags %>'
     $local_pref_length = '<%= @local_pref_length %>'
     $local_pref_value = '<%= @local_pref_value %>'
     $communities_flags = '<%= @communities_flags %>'
     $communities_length = '<%= @communities_length %>'
     $communities_value_1 = '<%= @communities_value_1 %>'
     $communities_value_2 = '<%= @communities_value_2 %>'
     $communities_value_3 = '<%= @communities_value_3 %>'
     $originator_flags = '<%= @originator_flags %>'
     $originator_length = '<%= @originator_length %>'
     $originator_id = '<%= @originator_id %>'
     $cluster_list_flags = '<%= @cluster_list_flags %>'
     $cluster_list_length = '<%= @cluster_list_length %>'
     $cluster_list = '<%= @cluster_list %>'
     $ext_communities_flags = '<%= @ext_communities_flags %>'
     $ext_communities_length = '<%= @ext_communities_length %>'
     $ext_communities_route_target = '<%= @ext_communities_route_target %>'
     $ext_communities_route_origin = '<%= @ext_communities_route_origin %>'"

    @base_variables = ''

    @base_transports = 'BGP = tcp(dst_port: 179)'

    if !filename.nil?
      template = get_message('BGP', filename)
      erb = ERB.new(template, 0, '%<>')
      @message = erb.result(binding)
    end
  end
end
